package com.zhuoyue.web.controller.system;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;


import com.zhuoyue.common.annotation.Log;
import com.zhuoyue.common.constant.HttpStatus;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.enums.BusinessType;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.system.domain.po.TblMessagePo;
import com.zhuoyue.system.domain.vo.TblMessageVo;
import com.zhuoyue.system.mapper.TblMessageMapper;
import com.zhuoyue.web.utils.OrgCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhuoyue.system.domain.TblMessage;
import com.zhuoyue.system.service.ITblMessageService;


/**
 * 消息Controller
 *
 * @author hewenze
 * @date 2020-08-13
 */
@RestController
@Api(value = "推送消息接口", tags = "推送消息接口")
@RequestMapping("/system/message")
public class TblMessageController extends BaseController {
    @Autowired
    private ITblMessageService tblMessageService;

    @Autowired
    private TblMessageMapper tblMessageMapper;

    @Autowired
    private OrgCodeUtils orgCodeUtils;

    /**
     * 查询消息列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询推送消息列表", notes = "查询推送消息列表")
    public TableDataInfo list(TblMessagePo tblMessagePo, HttpServletRequest request) {
        // System.out.println(tblMessagePo);
        tblMessagePo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        //startPage();
        Integer page = (tblMessagePo.getPageNum() - 1) * tblMessagePo.getPageSize();
        Integer size = tblMessagePo.getPageSize();

        Integer integer = this.tblMessageService.selectByTimeCount(tblMessagePo.getOrganizationCode(), tblMessagePo.getAccount(), tblMessagePo.getStartTime(), tblMessagePo.getEndTime());
        List<TblMessage> tblMessages = tblMessageService.selectByTime(tblMessagePo.getOrganizationCode(), tblMessagePo.getAccount(), tblMessagePo.getStartTime(), tblMessagePo.getEndTime(), page, size);
        //System.out.println(tblMessages);
        // List<TblMessageVo> list = tblMessageService.selectTblMessageList(tblMessagePo);
        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setTotal(integer);
        dataTable.setRows(tblMessages);
        dataTable.setCode(HttpStatus.SUCCESS);
        dataTable.setMsg("查询成功");

        return dataTable;
        //return getDataTable(tblMessages);
    }

    /**
     * 导出消息列表
     */
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出推送消息列表", notes = "导出推送消息列表")
    public void export(HttpServletRequest request, TblMessagePo tblMessagePo) throws IOException {

        tblMessagePo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        List<TblMessageVo> list = tblMessageService.selectTblMessageList(tblMessagePo);
        ExcelUtil<TblMessageVo> util = new ExcelUtil<TblMessageVo>(TblMessageVo.class);
        //util.exportExcel(response, list, "message");
    }

    /**
     * 获取推送消息数量
     */
    @ApiOperation(value = "查询推送消息数量", notes = "查询推送消息数量")
    @GetMapping(value = "MessageCount")
    public AjaxResult getMessageCount(HttpServletRequest request, TblMessagePo tblMessagePo) {

        tblMessagePo.setOrganizationCode(orgCodeUtils.getOrgCode(request));
        Integer integer = this.tblMessageService.selectByTimeCount(tblMessagePo.getOrganizationCode(), tblMessagePo.getAccount(), tblMessagePo.getStartTime(), tblMessagePo.getEndTime());

        //tblMessageMapper.selectByTimeCount(tblMessagePo.getOrganizationCode(),null,null,null)
        return AjaxResult.success(integer);
    }
}
